﻿$(function(){

anonymouslogin()


// ------------------------云函数调用  begin-------------------------

var hdPlay = {

	gethditem:function(id,fun){

		HDDB.gethditem(id,function (res){
			fun(res)
		})
	},
	addZanmeiFangWen:function(data,fun){
		wxCloud.cloud().callFunction({
			name:'addZanmeiFangWen',
			data:data,
			success:function(res){
				fun(res)
			}
		})
	}
}


//------------------------云函数调用 end-------------------------



//------------------------构建页面 begin-------------------------


var layer =  layui.layer

var index = layer.load(3, {
  shade: [0.1,'#fff'] //0.1透明度的白色背景
});


hdPlay.gethditem(parseInt(domain.getcanshu().id),function(res){

	layer.close(index)

	var pageData = res.result.list[0]
	var addData = {type:'hd',hd_id:pageData._id,mulu:pageData.title}


	wxSDK().then(res=>{
		SDKyanzheng(res).then(res=>{
			let title = pageData.title
			let url = location.href
			let imgUrl = $('#zhandianIcon').attr('href')
			let desc='少儿日志第' +  pageData._id + '期'
			fenxiang(title,url,imgUrl,desc)
		})
	})

	// 设置标题

	$('title').text( pageData._id + ' ' + pageData.title)
	$('#hd_title_text').text(pageData.title)

	// 渲染歌词
	var lyricArr = pageData.zm[0].lyric.split('\n')

	lyricArr.unshift(pageData.zm[0].mulu)
	zm_arrty = doT.template($('#zm_tep').text())
	$('.hd_zmimg').html(zm_arrty(lyricArr))

	//设置赞美音频
	var bb = jm(pageData.zm[0].xuhao + pageData.zm[0].mulu)
	console.log(bb);
	var url = ZMMADIAURI+bb+".mp3"
	$('#play_zm').attr({"data-url":url})
	// 渲染中英文经文 服务器url
	$('.hd_jwch .hd_jw_context').text(pageData.jw[0].chContext)
	$('.hd_jwch .hd_jw_sou').text(pageData.jw[0].chTitle)
	if(pageData.wrapper[0].audioUrl == ''){
		$('#ch_play').css('display','none')
	}else{
		$('#ch_play').attr('data-url', pageData.wrapper[0].audioUrl)
	}
	$('.hd_jwen .hd_jw_context').text(pageData.jw[0].enContext)
	$('.hd_jwen .hd_jw_sou').text(pageData.jw[0].enTitle)
	if(pageData.wrapper[1].audioUrl == ''){
		$('#en_play').css('display','none')
	}else{
		$('#en_play').attr('data-url', pageData.wrapper[1].audioUrl)
	}
	// 渲染图片
	$('#chImg').attr('src', pageData.wrapper[0].imgUrl)
	$('#enImg').attr('src', pageData.wrapper[1].imgUrl)

	//音频设置
	adInit('#play_zm')
	adInit('#en_play')
	adInit('#ch_play')

})


//------------------------构建页面 end-------------------------

//------------------------音频设置 begin-------------------------
function adInit(ele){

	var audio = $('audio')[0]



	$(ele).click(function()
	{

		if( $(this).attr('class') == 'iconfont icon-zanting1 float-right' ) //audio状态处于播放时
		{

			audio.pause()

			$(this).attr('class','iconfont icon-bofang1 float-right')

			return
		}


		if( $(this).attr('class') == 'iconfont icon-bofang1 float-right' )//audio状态处于暂停时
		{

			if(audio.src !== '') //audio判断是否有上一个播放url
			{


				if(audio.src == $(this).data()['url']) //audio判断当前点击是否和上一个播放url相同
				{
					audio.play()

					$(this).attr('class','iconfont icon-zanting1 float-right')
				}
				else
				{

					audio.src = $(this).data()['url']

					$(audio.pervEle).attr('class','iconfont icon-bofang1 float-right')

					audio.play()

					$(this).attr('class','iconfont icon-zanting1 float-right')

					audio.pervEle = ele

					audioEvent()

				}


			}
			else
			{

				audio.src = $(this).data()['url']

				audio.play()

				$(this).attr('class','iconfont icon-zanting1 float-right')

				audio.pervUrl = $(this).data()['url']

				audioEvent()

			}

			return
		}



	})


	function audioEvent() //设置音频播放事件
	{
		var layer =  layui.layer
		audio.onerror = function(){
			layer.msg('音频播放失败')
			$(this.pervEle).attr('class','iconfont icon-bofang1 float-right')

		}
		audio.onended = function(){

			$(this.pervEle).attr('class','iconfont icon-bofang1 float-right')

		}
	}
}

//------------------------音频设置 end-------------------------

})
